<template>
  <a-spin :spinning="confirmLoading">
    <JFormContainer :disabled="disabled">
      <template #detail>
        <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol" name="FInvoiceManagementForm">
          <a-row>
            <a-col :span="24">
              <a-form-item label="报销人" v-bind="validateInfos.reimbursement">
                <j-input v-model:value="formData.reimbursement" placeholder="请输入报销人"  allow-clear ></j-input>
              </a-form-item>
            </a-col>
						<a-col :span="24">
							<a-form-item label="发票代码" v-bind="validateInfos.invoiceCode" id="FInvoiceManagementForm-invoiceCode" name="invoiceCode">
								<a-input v-model:value="formData.invoiceCode" placeholder="请输入发票代码"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="发票号码" v-bind="validateInfos.invoiceNumber" id="FInvoiceManagementForm-invoiceNumber" name="invoiceNumber">
								<a-input v-model:value="formData.invoiceNumber" placeholder="请输入发票号码"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="数电票号码" v-bind="validateInfos.electronicInvoiceNumber" id="FInvoiceManagementForm-electronicInvoiceNumber" name="electronicInvoiceNumber">
								<a-input v-model:value="formData.electronicInvoiceNumber" placeholder="请输入数电票号码"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="销方识别号" v-bind="validateInfos.sellerIdentificationNumber" id="FInvoiceManagementForm-sellerIdentificationNumber" name="sellerIdentificationNumber">
								<a-input v-model:value="formData.sellerIdentificationNumber" placeholder="请输入销方识别号"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="销方名称" v-bind="validateInfos.sellerName" id="FInvoiceManagementForm-sellerName" name="sellerName">
								<a-input v-model:value="formData.sellerName" placeholder="请输入销方名称"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="购方识别号" v-bind="validateInfos.buyerIdentificationNumber" id="FInvoiceManagementForm-buyerIdentificationNumber" name="buyerIdentificationNumber">
								<a-input v-model:value="formData.buyerIdentificationNumber" placeholder="请输入购方识别号"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="购买方名称" v-bind="validateInfos.buyerName" id="FInvoiceManagementForm-buyerName" name="buyerName">
								<a-input v-model:value="formData.buyerName" placeholder="请输入购买方名称"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="开票日期" v-bind="validateInfos.issueDate" id="FInvoiceManagementForm-issueDate" name="issueDate">
								<a-date-picker placeholder="请选择开票日期"  v-model:value="formData.issueDate" showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="金额" v-bind="validateInfos.amount" id="FInvoiceManagementForm-amount" name="amount">
								<a-input-number v-model:value="formData.amount" placeholder="请输入金额" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="税额" v-bind="validateInfos.tax" id="FInvoiceManagementForm-tax" name="tax">
								<a-input-number v-model:value="formData.tax" placeholder="请输入税额" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="价税合计" v-bind="validateInfos.totalPriceWithTax" id="FInvoiceManagementForm-totalPriceWithTax" name="totalPriceWithTax">
								<a-input-number v-model:value="formData.totalPriceWithTax" placeholder="请输入价税合计" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="发票来源" v-bind="validateInfos.invoiceSource" id="FInvoiceManagementForm-invoiceSource" name="invoiceSource">
								<a-input v-model:value="formData.invoiceSource" placeholder="请输入发票来源"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="发票状态" v-bind="validateInfos.invoiceType" id="FInvoiceManagementForm-invoiceType" name="invoiceType">
								<a-input v-model:value="formData.invoiceType" placeholder="请输入发票状态"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="是否正数发票" v-bind="validateInfos.isPositiveInvoice" id="FInvoiceManagementForm-isPositiveInvoice" name="isPositiveInvoice">
								<a-input v-model:value="formData.isPositiveInvoice" placeholder="请输入是否正数发票"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="发票风险等级" v-bind="validateInfos.invoiceRiskLevel" id="FInvoiceManagementForm-invoiceRiskLevel" name="invoiceRiskLevel">
								<a-input v-model:value="formData.invoiceRiskLevel" placeholder="请输入发票风险等级"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="开票人" v-bind="validateInfos.issuerName" id="FInvoiceManagementForm-issuerName" name="issuerName">
								<a-input v-model:value="formData.issuerName" placeholder="请输入开票人"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="备注" v-bind="validateInfos.remarks" id="FInvoiceManagementForm-remarks" name="remarks">
								<a-input v-model:value="formData.remarks" placeholder="请输入备注"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="报销状态" v-bind="validateInfos.status" id="FInvoiceManagementForm-status" name="status">
								<a-input v-model:value="formData.status" placeholder="请输入报销状态"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
          </a-row>
        </a-form>
      </template>
    </JFormContainer>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getValueType } from '/@/utils';
  import { Form } from 'ant-design-vue';
  import JFormContainer from '/@/components/Form/src/container/JFormContainer.vue';
  import JInput from "@/components/Form/src/jeecg/components/JInput.vue";
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: () => ({})},
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    reimbursement:'',
    invoiceCode: '',   
    invoiceNumber: '',   
    electronicInvoiceNumber: '',   
    sellerIdentificationNumber: '',   
    sellerName: '',   
    buyerIdentificationNumber: '',   
    buyerName: '',   
    issueDate: '',   
    amount: undefined,
    tax: undefined,
    totalPriceWithTax: undefined,
    invoiceSource: '',   
    invoiceType: '',   
    isPositiveInvoice: '',   
    invoiceRiskLevel: '',   
    issuerName: '',   
    remarks: '',   
    status: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = reactive({
    electronicInvoiceNumber: [{ required: true, message: '请输入数电票号码!'},],
  });
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    try {
      // 触发表单验证
      await validate();
    } catch ({ errorFields }) {
      if (errorFields) {
        const firstField = errorFields[0];
        if (firstField) {
          formRef.value.scrollToField(firstField.name, { behavior: 'smooth', block: 'center' });
        }
      }
      return Promise.reject(errorFields);
    }
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
  //   await saveOrUpdate(model, isUpdate.value)
  //     .then((res) => {
  //       if (res.success) {
  //         createMessage.success(res.message);
  //         emit('ok');
  //       } else {
  //         createMessage.warning(res.message);
  //       }
  //     })
  //     .finally(() => {
  //       confirmLoading.value = false;
  //     });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    padding: 14px;
  }
</style>
